/**
 * http://www.herongyang.com/Cryptography/MD5-Message-Digest-in-Java.html
 * http://www.twmacinta.com/myjava/fast_md5.php
 * 
 * Created on 24 Apr. 2011 @ 01:13:51 by Laurens Andriessen (483862@student.inholland.nl)
 */

package tests.laurens;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;


public class MD5 {

	private static MessageDigest digester;

	static {
		try {
			digester = MessageDigest.getInstance("MD5");
		} catch(NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
	}

	public static String crypt(String str) {
		if(str == null || str.length() == 0) { throw new IllegalArgumentException("String to encript cannot be null or zero length"); }

		digester.update(str.getBytes());
		byte[] hash = digester.digest();
		StringBuffer hexString = new StringBuffer();
		for(int i = 0; i < hash.length; i++) {
			if((0xff & hash[i]) < 0x10) {
				hexString.append("0" + Integer.toHexString((0xFF & hash[i])));
			} else {
				hexString.append(Integer.toHexString(0xFF & hash[i]));
			}
		}
		return hexString.toString();
	}

	public static void main(String args[]) {
		System.out.println(MD5.crypt("0000"));
	}
}
